package 多线程.Test;

import java.util.Arrays;
import java.util.Comparator;

public class PromotionAlgorithm {

  public static int calculateMaxTime(int rechargeAmount, int packagePrice, int[][] policies) {

    int basicTime = (int)Math.floor(rechargeAmount / (double)packagePrice);
    
    int maxTime = basicTime;
    int remainingTime = basicTime;
    
    Arrays.sort(policies, Comparator.comparingInt(p -> p[0]));
    
    for(int[] policy : policies) {
      
      int minTime = policy[0];
      int multiple = policy[1];  
      int bonus = policy[2];

      if(remainingTime >= minTime) {
        
        maxTime += bonus;
        
        if(multiple == 1) {
          remainingTime -= minTime;
        } else {
          remainingTime = basicTime; 
        }
        
      }
      
    }
    
    return maxTime;
  }

  public static void main(String[] args) {
    
    int rechargeAmount = 50;
    int packagePrice = 4;
    
    int[][] policies = {
      {2, 1, 0}, 
      {3, 0, 1},
      {5, 1, 2}
    };
    
    System.out.println(calculateMaxTime(rechargeAmount, packagePrice, policies));
  }

}